
<template name="sendcar">
  <!-- 移位的物资列表 -->
  <view class="mypage">
    <view class="cu-bar search bg-white">
      <view class="search-form round">
        <text class="cuIcon-search"></text>
        <input type="text" placeholder="请输入物资名称" v-model="keyword" />
        <input type="text" placeholder="请输入物资规格" v-model="keyword2" />
      </view>
      <view class="action">
        <button class="cu-btn bg-blue shadow-blur round" @tap="handelSearch">搜索</button>
      </view>
    </view>

    <view class="card-list-wrapper">
      <view
        class="card-list-row radius shadow-warp bg-white margin-top"
        v-for="(item,index) in planList"
        :key="index"
        @click="openDetail(index)"
      >
        <view class="flex-just-between bottom-border top-wrap">
          <text class="text-blue text-bold">物资单号：{{ item[config.materId] || '' }}</text>
          <view class="cu-capsule radius">
            <view class="cu-tag bg-blue sm">
              <text class="cuIcon-medal"></text>
            </view>
            <view class="cu-tag line-brown sm">{{ index+1}}</view>
          </view>
        </view>

        <view class="card-list-row-1 margin-top">
          <text class="cuIcon-title text-red"></text>
          <text>名称：{{ item[config.materName] || '' }}</text>
        </view>

        <view class="card-list-row-1">
          <text class="cuIcon-title text-orange"></text>
          <text>规格：{{ item.MATSPEC || '' }}</text>
        </view>

        <view class v-if="category=='ST_STOCKINFO' ">
          <view class="card-list-row-1">
            <text class="cuIcon-title text-orange"></text>
            <text>库存总量：{{ item.TOTALNUM  }}({{ item.UNIT || '' }})</text>
          </view>
					<view class="card-list-row-1">
					  <text class="cuIcon-title text-orange"></text>
					  <text>货位：{{ item.STATIONNAME }})</text>
					</view>
          <!-- <view class="card-list-row-1" v-for="(itemS,indexS) in mapSattionName(item.VBDEF18)">
            <text class="cuIcon-title text-red"></text>
            <text>{{ indexS+1 }}号货位：{{ itemS.STATIONNAME || '' }}/{{ itemS.KCNUM }} {{ item.UNIT }}</text>
          </view> -->
        </view>

        <view class="position-a checkFlag" v-if="item.ischeck">
          <text class="cuIcon-squarecheckfill text-blue"></text>
        </view>
      </view>
    </view>

    <view class="next-btn-wrap position-f" @click="nextTap">
      <button class="big-btn bg-green shadow-blur">
        <text class="cuIcon-refresharrow text-bold"></text>
      </button>
    </view>

    <modal
      title="高级查询"
      placeholder
      :isShow="isShowModal"
      @cancelModal="cancelModal"
      @okModal="okModal"
    ></modal>
  </view>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import api from "@/common/api.js";
import utils from "@/common/util.js";
import modal from "@/components/modal/modal_mater.vue";
let query = {
  category: "",
  page: 1,
  rows: "10",
  sidx: "CREATIONTIME",
  sord: "desc",
  queryJson: "",
	TYPE:"",
  userId: "",
  account: ""
};
let _self,
  menuType,
  page = 1,
  billID,
  total = 1,
  goPath = "",
  pageTitle = "基础物资列表(",
	PK_STORE = '',
  queryJson = {} ;

export default {
  computed: mapState(["userInfo", "baseUrl", "checkData"]),
  data() {
    return {
      planList: [],
      keyword: "",
      keyword2: "",
      category: "",
      isShowModal: false,
      config: {
        materId: "",
        materName: ""
      }
    };
  },

  onLoad(p) {
    // console.log(p)
    _self = this;
		this.setCheckData([]);
    menuType = 'ST_STOCKINFO';
		PK_STORE = p.PK_STORE
		queryJson.PK_STORE = p.PK_STORE
    this.initData();
  },
  components: {
    modal
  },
  onNavigationBarButtonTap() {
    this.isShowModal = true;
  },
  onShow() {
    // 刷新列表
    uni.$once("refreshList", () => {
      page = 1;
      total = 1;
      this.getList();
    });
  },
  //下拉刷新
  onPullDownRefresh: function() {
    // 重置分页及数据
    page = 1;
    total = 1;
		_self.planList = [];
		// queryJson.PK_ORG = _self.userInfo.ORGANIZEID;
    // queryJson = "";
    this.getList();
    setTimeout(function() {
      uni.stopPullDownRefresh();
    }, 1000);
    uni.showToast({
      title: "数据已刷新",
      icon: "none"
    });
  },
  // 加载更多
  onReachBottom: function() {
    this.getList();
  },
  methods: {
    ...mapMutations(["setCheckData"]),
    initData() {
			query.category = "ST_STOCKINFO";
			query.TYPE = ""
			this.config.materId = "PK_MATERIAL";
			this.config.materName = "MATERIALNAME";
			pageTitle = "库存物资列表(";
			
      // 判断要跳转的 MATSPEC
      goPath = "/pages/tab01/inbound/submit-manual?data=";
      switch (menuType) {
        case "applyIn": // 新增入库申请单
          goPath = "/pages/tab01/inbound/submit-apply?data=";
          break;
        case "manualIn": // 手工入库
          goPath = "/pages/tab01/inbound/submit-manual?data=";
          break;
        case "applyOut": // 新增 出库申请单
          goPath = "/pages/tab01/outbound/submit-apply?data=";
          break;
        case "manualOut": // 手工出库单
          goPath = "/pages/tab01/outbound/submit-manual?data=";
          break;
        default:
          break;
      }
      // 手工单出库 + 新增出库申请单 使用库存物资
      query.userId = _self.userInfo.UserId;
      query.account = _self.userInfo.Account;
      queryJson.PK_ORG = _self.userInfo.ORGANIZEID;
      this.category = query.category;
      page = 1;
      total = 1;
      this.getList();
    },
		
		// STATIONNAME 遍历仓库
		
		mapSattionName(data){
			if(!data) return [];
			let arr = JSON.parse(data);
			return arr ;
		},
		
    openDetail(e) {
      _self.planList[e].ischeck = !_self.planList[e].ischeck;
      _self.planList[e].myIndex = e;
    },
    handelSearch() {
      // queryJson.BILLNO = _self.keyword;
    //   let a = {
    //     MATSPEC: _self.keyword2,
    //     MATERIALNAME: _self.keyword,
    //     VNAME: _self.keyword,
    //     BILLNO: "",
				// PK_ORG: _self.userInfo.ORGANIZEID
    //   };
		//  queryJson = { ...a };
		
			queryJson.MATSPEC = _self.keyword2
			queryJson.MATERIALNAME = _self.keyword
			queryJson.VNAME = _self.keyword
			queryJson.PK_STORE = PK_STORE  || ''
			queryJson.PK_ORG = _self.userInfo.ORGANIZEID,
			
      page = 1;
      total = 1;
      _self.planList = [];
      this.getList();
    },
    nextTap() {
      let t = [];
      _self.planList.forEach(item => {
        if (item.ischeck) {
          t.push(item);
        }
      });
			let newCheckData = []; // 如果新选择的数据不存在 已选择中 进行添加到 chekData
			let oldCheckDataPK = '';
			_self.checkData.forEach(item=>{
				oldCheckDataPK += item.PK_MATERIAL +'-';
			})
			
			if(_self.checkData.length==0){
				newCheckData = [...t]
			}else{
				t.forEach(item=>{
					if(oldCheckDataPK.indexOf(item.PK_MATERIAL) <0){
						newCheckData.push(item)
					}
				})
			}
			let newCheckData2 = [...newCheckData,...this.checkData ] ;
      this.setCheckData(newCheckData2);
			if (newCheckData2.length < 1) return _self.$utils.tips("请至少选择一个物资！");
			uni.navigateBack({})
      // uni.navigateTo({
      //   url: goPath + encodeURIComponent(JSON.stringify(t))
      // });
    },
		
    getList() {
      // console.log(page , total);
      if (page > total) {
        return utils.tips("没有数据了");
      }
      query.page = page;
      query.queryJson = JSON.stringify(queryJson);
      let URL = api.apiPublic.getMaterList;
			this.axios({
				url: URL ,
				data: query ,
				isLoading: 1
			}).then(res=>{
				let { Result } = res ;
				let array = Result.rows.map(item => {
				  item.ischeck = false;
					item.is_check = false // 是否验证物资
					item.INSTATIONNAME = ''
					item.PK_INSTATIONNAME = ''
					item.OUTPHOTO = ''
					item.BILLNO = ''
					item.PK_MATERIAL = item.PK_MATERIAL || item.BILLNO
				  return item;
				});
				if (page == 1) {
				  _self.planList = array;
				  if (_self.planList.length == 0) {
				    utils.tips("暂无数据！");
				  }
				  _self.setPageTitle(pageTitle + Result.records + ")");
				} else {
				  _self.planList = _self.planList.concat(array);
				}
				queryJson = {};
				total = Result.total;
				page++;
			})
    },
    setPageTitle(d) {
      uni.setNavigationBarTitle({
        title: d
      });
    },

    // 控制高级查询条件
    cancelModal() {
      this.isShowModal = false;
    },
    okModal(val) {
      this.cancelModal();
      queryJson = { ...val };
      page = 1;
      total = 1;
      _self.planList = [];
      this.getList();
    }
  }
};
</script>

<style>
page{
	padding-bottom: 100rpx;
	box-sizing: border-box;
}
.card-list-wrapper {
  width: 710upx;
  margin: 0 auto;
}

.card-list-row {
  padding: 20upx;
}

.card-list-row-1 {
  padding: 10upx 0;
}



.top-border {
  border-top: 1upx solid rgba(0, 0, 0, 0.1);
}

.top-wrap {
  padding-bottom: 20upx;
}

.bottom-border {
  border-bottom: 1upx solid rgba(0, 0, 0, 0.1);
}

.btn-wrap {
  padding-top: 20upx;
}

.checkFlag {
  top: 50%;
  right: 20upx;
  font-size: 40upx;
}

.next-btn-wrap {
  position: fixed;
  right: 50upx;
  bottom: 100upx;
}

.big-btn {
  width: 100upx;
  height: 100upx;
  border-radius: 50upx;
  font-size: 40upx;
}
</style>
